package td;

import java.util.ArrayList;

/*
 * Class for player. Manages tower/trap/magicstone picking and placing, gaining mana for kills
 * and losing hp for each enemy that can reach the endpoint.
 * */
public class Player {
	/* Stores player hp */
	private int hp;
	/* Stores player mana */
	private int mana;
	/* Stores the FieldElement the player has selected */
	protected FieldElement selected;
	/* Stores the MagicStones the player has selected */
	protected MagicStones pickedStone;

	public ArrayList<Tower> towers = new ArrayList<Tower>();
	public ArrayList<VineTrap> vineTraps = new ArrayList<VineTrap>();
	public ArrayList<WindTrap> windTraps = new ArrayList<WindTrap>();
	public ArrayList<FireTrap> fireTraps = new ArrayList<FireTrap>();

	public boolean vineTrapOnCD = false;
	public boolean windTrapOnCD = false;
	public boolean fireTrapOnCD = false;

	public Player(int mana, int hp) {
		this.mana = mana;
		this.hp = hp;
	}

	public void pickTower(Tower selected) {
		this.selected = selected;
	}

	public void placeTower(Field field) {
		Tower placed = (Tower) selected;
		field.addElement(placed);
		field.hasTower = true;
		mana -= placed.cost;
		towers.add(placed);
	}

	public void pickTrap(Trap selected) {
		this.selected = selected;
	}

	public void placeTrap(Field field) {
		field.addElement((Trap) selected);
		field.hasTrap = true;
		if (selected instanceof VineTrap) {
			vineTrapOnCD = true;
			vineTraps.add((VineTrap)selected);
		}
		if (selected instanceof WindTrap) {
			windTrapOnCD = true;
			windTraps.add((WindTrap)selected);
		}
		if (selected instanceof FireTrap) {
			fireTrapOnCD = true;
			fireTraps.add((FireTrap)selected);
		}
	}

	public void pickMagicStone(MagicStones selected) {
		this.pickedStone = selected;
	}

	/*
	 * Upgrades selected Tower. Since MagicStones is only an enumeration, we
	 * can't ask their cost, so we remove it here manually
	 */
	public void placeMagicStone(Tower powered, int place) {
		powered.upgrade(this.pickedStone, place);
		powered.viewable.update();
		switch (this.pickedStone) {
		case Blue:
			mana -= 50;
			break;
		case Red:
			mana -= 60;
			break;
		case Yellow:
			mana -= 50;
			break;
		case Rainbow:
			mana -= 100;
			break;
		}
	}

	/*
	 * Upgrades selected Trap. Since MagicStones is only an enumeration, we
	 * can't ask their cost, so we remove it here manually
	 */
	public void placeMagicStone(Trap powered) {
		powered.upgrade(this.pickedStone);
		powered.viewable.update();
		switch (this.pickedStone) {
		case Blue:
			mana -= 50;
			break;
		case Red:
			mana -= 60;
			break;
		case Yellow:
			mana -= 50;
			break;
		case Rainbow:
			mana -= 100;
			break;
		}
	}

	public void gainMana(Tower sold) {
		mana += (sold.cost / 2);
	}

	public void gainMana(Enemy killed) {
		mana += killed.value;
	}

	public void loseHp(Enemy enemy) {
		hp -= enemy.damage;
	}

	public int getMana() {
		return mana;
	}

	public int getHp() {
		return hp;
	}

	/*
	 * Manages tower selling. Gives mana(half of tower cost), and removes tower
	 * from field.
	 */
	public void sellTower(Tower selected) {
		gainMana(selected);
		selected.position.removeElement(selected);
		selected.position.hasTower = false;
		towers.remove(selected);
	}
	
	public FieldElement getSelected()
	{
		return selected;
	}
}
